package org.smartercontext.contextmger.communication;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartercontext.contextmger.fixedgatherers.BuyerPreferences;
import org.smartercontext.contextmger.fixedgatherers.ExternalLogin;
import org.smartercontext.contextmger.fixedgatherers.IBuyerPreferences;
import org.smartercontext.contextmger.fixedgatherers.IExternalLogin;
import org.smartercontext.contextmger.fixedgatherers.ILocationGatherer;
import org.smartercontext.contextmger.fixedgatherers.LocationGatherer;
import org.smartercontext.contextmger.providers.IProvider;
import org.smartercontext.contextmger.providers.Provider;


/**
 * 
 * @author JuanPablo
 * @version 1.0
 * @creation_date 09 Mar 2014
 * This class allows to the Server receive requests of external apps.
 *
 */
@WebService(name="Communication_Server", targetNamespace="http://www.smartercontext.org/contextmgr/communication/")
public class Communication_Impl implements ICommunication{

	@WebMethod(operationName="login", action="http://www.smartercontext.org/contextmgr/communication/ExternalLLogin")
	@Override
	public boolean login(@WebParam(name="credentials")
	String loginT, @WebParam(name="format")String format) {
		
		IExternalLogin externalLogin = new ExternalLogin();
		
		String username="";
		String password="";		
		
		if(format.equals("json")){
			JSONObject object;
			try {
				object = new JSONObject(loginT);
			
			username = object.getString("userName");
			password = object.getString("password");
			boolean status=externalLogin.login(username, password);
			return status;
			
			} catch (JSONException e) {
				
				return false;
			}
		}else{
			//For future developments (e.g: xml)
		}
		
		return externalLogin.login(username, password);
	}

	@WebMethod(operationName="receiveBuyerPreferences", action="http://www.smartercontext.org/contextmger/communication/ReceiveBuyerPreferences")
	@Override
	public void receiveBuyerPreferences(@WebParam(name="preferences")
	String preferences, @WebParam(name="format")String format) {
		
		if(format.equals("json")){
			try {
				JSONObject object = new JSONObject(preferences);
				String username = object.getString("username");
				
				JSONArray jsonArrayWish = object.getJSONArray("wishlist");
				List<String>wishlist = new ArrayList<>();
				
				for(int i=0;i<jsonArrayWish.length();i++){
					JSONObject temp = jsonArrayWish.getJSONObject(i);
					String productname = temp.getString("product-name");
					String eanucc = temp.getString("ean-ucc");
					String gtin8 = temp.getString("gtin-8");
					String gtin14 = temp.getString("gtin14");
					String product = productname+"#"+eanucc+"#"+gtin8+"#"+gtin14;
					wishlist.add(product);
				}
					
				
				JSONArray jsonArrayPSeen = object.getJSONArray("products-seen");
				List<String>prodSeen = new ArrayList<>();
				
				for(int i=0;i<jsonArrayPSeen.length();i++){
					JSONObject temp = jsonArrayPSeen.getJSONObject(i);
					String productname = temp.getString("product-name");
					String eanucc = temp.getString("ean-ucc");
					String gtin8 = temp.getString("gtin-8");
					String gtin14 = temp.getString("gtin14");
					String product = productname+"#"+eanucc+"#"+gtin8+"#"+gtin14;
					prodSeen.add(product);
				}
				
				JSONArray jsonArrayPRank = object.getJSONArray("products-ranked");
				List<String>pranked = new ArrayList<>();
				
				for(int i=0;i<jsonArrayPRank.length();i++){
					JSONObject temp = jsonArrayPRank.getJSONObject(i);
					String value = temp.getString("value");
					String productname = temp.getString("product-name");
					String eanucc = temp.getString("ean-ucc");
					String gtin8 = temp.getString("gtin-8");
					String gtin14 = temp.getString("gtin14");
					String product = value+"#"+productname+"#"+eanucc+"#"+gtin8+"#"+gtin14;
					pranked.add(product);
				}
				
				IBuyerPreferences buyerPrefs = new BuyerPreferences();
				buyerPrefs.savePreferences(wishlist, prodSeen, pranked, username);
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			//For next developments (e.g: xml)
		}
		
	}

	@WebMethod(operationName="receiveCurrentLocation", action="http://www.smartercontext.org/contextmger/communication/ReceiveCurrentLocation")
	@Override
	public void receiveCurrentLocation(@WebParam(name="location")String location,
			@WebParam(name="format")String format) {
		
				
		ILocationGatherer locationGatherer = new LocationGatherer();
		
		if(format.equals("json")){
			JSONObject objLocation;
			try {
				objLocation = new JSONObject(location);
			
			String latitude = objLocation.getString("latitude");
			String longitude = objLocation.getString("longitude");
			String user = objLocation.getString("user");	
			locationGatherer.receiveCurrentLocation(latitude, longitude, user);
			
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			//For next developments (e.g: xml)
		}
		
		
		
	}

	@WebMethod(operationName="provideContext", action="http://www.smartercontext.org/contextmger/communication/ProvideContext")
	@Override
	public String provideContext(@WebParam(name="username")String username, 
			@WebParam(name="typeInf")String typeInf, @WebParam(name="format")String format) {
		
		IProvider provider = new Provider();
		String context = provider.provideContext(username, typeInf, format);
		
		String returnInf = "";
		
		if(context==null){
			returnInf = "It's impossible to acquire context, try again!";
		}else{
			returnInf = context;
		}
		
		return returnInf;
	}

}
